//
//  OMTNavigationBar.swift
//  OMTUIKitSwift
//
//  Created by yizhaorong on 2018/12/8.
//

import UIKit
import SnapKit


// 默认按钮字体大小
let DEFAULT_BUTTON_TITLE_FONT_SIZE: CGFloat = 16
// 默认标题大小
let DEFAULT_TITLE_SIZE: CGFloat = 17
// 标题颜色
let DEFAULT_TITLE_COLOR: UIColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

public class OMTNavigationBar: UIControl {
    // MARK: 公开属性
    // 标题
    public var title: String! {
        didSet {
            if (self.titleView as AnyObject).isKind(of: UILabel.self) {
                let titleView = self.titleView as! UILabel
                titleView.text = self.title
            }
        }
    }
    // 标题颜色
    public var titleColor: UIColor! = DEFAULT_TITLE_COLOR {
        didSet {
            if (self.titleView as AnyObject).isKind(of: UILabel.self) {
                let titleView = self.titleView as! UILabel
                titleView.textColor = self.titleColor
            }
        }
    }
    // 标题字体
    public var titleFont: UIFont! = UIFont.boldSystemFont(ofSize: DEFAULT_TITLE_SIZE) {
        didSet {
            if (self.titleView as AnyObject).isKind(of: UILabel.self) {
                let titleView = self.titleView as! UILabel
                titleView.font = self.titleFont
            }
        }
    }
    // 背景图片
    public var backgroundImage: UIImage! {
        didSet {
            self.backgroundImageView.isHidden = false
            self.backgroundImageView.image = self.backgroundImage
        }
    }
    // 内容区域
    private var contentView: UIView = UIView.init()
    // 标题视图
    public var titleView: UIView {
        willSet {
            self.titleView.removeFromSuperview()
        }
        didSet {
            self.contentView.addSubview(self.titleView)
            self.titleView.snp.makeConstraints { (make) in
                make.center.equalTo(self.contentView.snp.center)
                make.left.greaterThanOrEqualTo(self.leftButton.snp.right).offset(6)
                make.right.lessThanOrEqualTo(self.rightButton.snp.left).offset(-6)
            }
        }
    }
    // 显示投影
    public var showShadow: Bool = false {
        didSet {
            if self.showShadow {
                self.layer.shadowColor = UIColor.init(white: 0, alpha: 1).cgColor
                self.layer.shadowOffset = CGSize(width: 0, height: -5)
                self.layer.shadowRadius = 3
                self.layer.shadowOpacity = 1
            } else {
                self.layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0).cgColor
                self.layer.shadowOffset = CGSize(width: 0, height: 0)
                self.layer.shadowRadius = 0
                self.layer.shadowOpacity = 0
            }
        }
    }
    // 左侧按钮回调
    public var didClickLeftButtonBlock:((_ sender: UIButton) -> Void)?
    // 右侧按钮回调
    public var didClickRightButtonBlock:((_ sender: UIButton) -> Void)?
    
    // MARK: 私有属性
    // 底部线
    private lazy var bottomLine: UIView = {
        let bottomLine = UIView()
        bottomLine.backgroundColor = #colorLiteral(red: 0.8745098039, green: 0.8745098039, blue: 0.8745098039, alpha: 1)
        return bottomLine
    }()
    // 背景图片
    private lazy var backgroundImageView: UIImageView = {
        let backgroundImageView = UIImageView()
        backgroundImageView.isHidden = true
        return backgroundImageView
    }()
    
    // 左侧按钮
    public private(set) lazy var leftButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(leftButtonAction(_:)), for: .touchUpInside)
        button.imageView?.contentMode = .center
        button.setTitleColor(DEFAULT_TITLE_COLOR, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: DEFAULT_BUTTON_TITLE_FONT_SIZE)
        button.isHidden = true
        return button
    }()
    // 右侧按钮
    public private(set) lazy var rightButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(rightButtonAction(_:)), for: .touchUpInside)
        button.imageView?.contentMode = .center
        button.setTitleColor(DEFAULT_TITLE_COLOR, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: DEFAULT_BUTTON_TITLE_FONT_SIZE)
        button.isHidden = true
        return button
    }()
    
    // MARK: 初始化
    public init() {
        let titleView = UILabel.init()
        titleView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
        titleView.font = titleFont
        titleView.textColor = titleColor
        self.titleView = titleView
        
        super.init(frame: CGRect.zero)
        self.contentView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0)
        self.backgroundColor = #colorLiteral(red: 0.9764705882, green: 0.9764705882, blue: 0.9764705882, alpha: 1)
        self.setupSubviews()
        self.setupConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupSubviews() {
        self.addSubview(self.backgroundImageView)
        self.addSubview(self.contentView)
        self.addSubview(self.bottomLine)
        self.contentView.addSubview(self.leftButton)
        self.contentView.addSubview(self.rightButton)
        self.contentView.addSubview(self.titleView)
    }
    
    func setupConstraints() {
        self.backgroundImageView.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalTo(0)
        }
        self.contentView.snp.makeConstraints { (make) in
            make.height.equalTo(OMT_NAVIGATION_BAR_HEIGHT - OMT_STATUS_BAR_HEIGHT)
            make.bottom.left.right.equalTo(0)
        }
        self.bottomLine.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
        self.leftButton.snp.makeConstraints { (make) in
            make.left.equalTo(16)
            make.size.greaterThanOrEqualTo(CGSize(width: 44, height: 44))
            make.centerY.equalTo(self.contentView.snp.centerY)
        }
        self.rightButton.snp.makeConstraints { (make) in
            make.right.equalTo(-16)
            make.size.greaterThanOrEqualTo(CGSize(width: 44, height: 44))
            make.centerY.equalTo(self.contentView.snp.centerY)
        }
        
        self.titleView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        self.titleView.snp.makeConstraints { (make) in
            make.center.equalTo(self.contentView.snp.center)
            make.left.greaterThanOrEqualTo(self.leftButton.snp.right).offset(6)
            make.right.lessThanOrEqualTo(self.rightButton.snp.left).offset(-6)
        }
    }
    
    // MARK: Events
    @objc private func leftButtonAction(_ sender: UIButton) {
        if let block = self.didClickLeftButtonBlock {
            block(sender)
        }
    }
    
    @objc private func rightButtonAction(_ sender: UIButton) {
        if let block = self.didClickRightButtonBlock {
            block(sender)
        }
    }
    
    // MARK: 左侧按钮
    public func setLeftButton(normalImage: UIImage? = nil, highlightedImage: UIImage? = nil, title: String? = nil, titleColor: UIColor? = nil) {
        self.leftButton.isHidden = false
        self.leftButton.setImage(normalImage, for: .normal)
        self.leftButton.setImage(highlightedImage, for: .highlighted)
        var titleColor  = titleColor
        if titleColor == nil {
            titleColor = DEFAULT_TITLE_COLOR
        }
        self.leftButton.setTitleColor(titleColor, for: .normal)
        self.leftButton.setTitle(title, for: .normal)
        self.setNeedsLayout()
    }
    
    // MARK: 右侧按钮
    public func setRightButton(normalImage: UIImage? = nil, highlightedImage: UIImage? = nil, title: String? = nil, titleColor: UIColor? = nil) {
        self.rightButton.isHidden = false
        self.rightButton.setImage(normalImage, for: .normal)
        self.rightButton.setImage(highlightedImage, for: .highlighted)
        var titleColor  = titleColor
        if titleColor == nil {
            titleColor = DEFAULT_TITLE_COLOR
        }
        self.rightButton.setTitleColor(titleColor, for: .normal)
        self.rightButton.setTitle(title, for: .normal)
        self.setNeedsLayout()
    }
    
    // MARK: 重写方法
    public override func didMoveToSuperview() {
        super.didMoveToSuperview()
        if self.superview != nil {
            self.snp.makeConstraints { (make) in
                make.left.top.right.equalTo(0)
                make.height.equalTo(OMT_NAVIGATION_BAR_HEIGHT)
            }
        }
    }
    
}
